﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.PhongLenh
{
    public partial class ucDuyetLenh : UserControl
    {
        public ucDuyetLenh()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
            SetPropertiesColumns();}

        private void LoadData()
        {
            var tuNgay = dtTuNgay.Checked ? dtTuNgay.Value.ToString("MM/dd/yyyy") : DateTime.Now.Month + "/01/" + DateTime.Now.Year;
            var denNgay = dtDenNgay.Checked ? dtDenNgay.Value.ToString("MM/dd/yyyy") : DateTime.Now.Month + "/"
                + DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month) + "/" + DateTime.Now.Year;

            DataTable data = DataAll.SelectAll("LenhSanXuat inner join PhongMau_SanPham On LenhSanXuat.IdSanPham = PhongMau_SanPham.IdSanPham",
                "LenhSanXuat.[ID],PhongMau_SanPham.[MaHang] as MaSanPham,LenhSanXuat.[IdSanPham],[NgayLapLenh],[NguoiLapLenh],LenhSanXuat.[SoLuong],[NgayGuiLenh],[NgaySuaLenh],[NgayDuyetLenh],LenhSanXuat.[TrangThai],[NgayChungTu],[NgayHoachToan],LenhSanXuat.[SoChungTu],LenhSanXuat.[GhiChu],[KhoDen],[KhoDi],[SXTuNgay],[SXDenNgay]",
                "WHERE LenhSanXuat.TrangThai = " + Convert.ToInt32(TrangThai.LenhSanXuat_ChoDuyet));
            gridLenhChoDuyet.DataSource = data;

            DataTable dt = DataAll.SelectAll("LenhSanXuat Inner join PhongMau_SanPham On LenhSanXuat.IdSanPham = PhongMau_SanPham.IdSanPham",
                "LenhSanXuat.[ID],PhongMau_SanPham.[MaHang] as MaSanPham,LenhSanXuat.[IdSanPham],[NgayLapLenh],[NguoiLapLenh],LenhSanXuat.[SoLuong],[NgayGuiLenh],[NgaySuaLenh],[NgayDuyetLenh],LenhSanXuat.[TrangThai],[NgayChungTu],[NgayHoachToan],LenhSanXuat.[SoChungTu],LenhSanXuat.[GhiChu],[KhoDen],[KhoDi],[SXTuNgay],[SXDenNgay]",
                "WHERE (LenhSanXuat.TrangThai != " + Convert.ToInt32(TrangThai.LenhSanXuat_ChoDuyet) + ") AND (CONVERT(DATE, NgayDuyetLenh, 0) >= CONVERT(DATE,'"
                + tuNgay + "', 0) AND CONVERT(DATE, NgayDuyetLenh, 0) <= CONVERT(DATE,'" + denNgay + "' , 0))");
            gridLenh.DataSource = dt;
        }

        public void SetPropertiesGrid()
        {
            gvLenh.BestFitColumns();
            gvLenh.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvLenh.OptionsBehavior.Editable = false;
            //gvLenh.OptionsSelection.EnableAppearanceFocusedCell = true;
            gvLenh.OptionsSelection.MultiSelect = true;
            gvLenh.OptionsView.ShowGroupPanel = false;
            gvLenh.OptionsView.ColumnAutoWidth = false;
            gvLenh.OptionsView.ShowFooter = true;
            gvLenh.OptionsFind.AlwaysVisible = true;
            gvLenh.Columns["ID"].Visible = false;

            gvLenhChoDuyet.BestFitColumns();
            gvLenhChoDuyet.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvLenhChoDuyet.OptionsBehavior.Editable = false;
            //gvLenhChoDuyet.OptionsSelection.EnableAppearanceFocusedCell = true;
            gvLenhChoDuyet.OptionsSelection.MultiSelect = true;
            gvLenhChoDuyet.OptionsView.ShowGroupPanel = false;
            gvLenhChoDuyet.OptionsView.ColumnAutoWidth = false;
            gvLenhChoDuyet.OptionsView.ShowFooter = true;
            gvLenhChoDuyet.OptionsFind.AlwaysVisible = true;
            gvLenhChoDuyet.Columns["ID"].Visible = false;
            for (int i = 3; i < gvLenhChoDuyet.Columns.Count; i++)
            {
                gvLenhChoDuyet.Columns[i].OptionsColumn.AllowEdit = false;
            }
        }

        public void SetPropertiesColumns()
        {
            gcDuyetLenh.Width = 60;
            gcDuyetLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcKhongDuyet.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMaSanPham1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayLapLenh1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNguoiLapLenh1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayGuiLenh1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTrangThai1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayChungTu1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayHoachToan1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoChungTu1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;


            gcMaSanPham.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNguoiLapLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayDuyetLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayGuiLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTrangThai.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayHoachToan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoChungTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        }

        private void btnDuyetLenh_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvLenhChoDuyet.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn duyệt lệnh này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("LenhSanXuat", "TrangThai =" + Convert.ToInt32(TrangThai.LenhSanXuat_ChotLenh) + ",NgayDuyetLenh ='" + DateTime.Now.ToString("MM/dd/yyyy") + "' WHERE ID =" + dataview[gvLenhChoDuyet.FocusedRowHandle]["ID"]);
                if (message.Equals("success"))
                {
                    var data = DataAll.SelectAll("HanhTrinh", "*", " WHERE SoChungTu='" + dataview[gvLenhChoDuyet.FocusedRowHandle]["SoChungTu"]
                    + "' AND TrangThai=" + Convert.ToInt32(TrangThai.LenhSanXuat_ChotLenh));
                    if (data.Rows.Count <= 0)
                    {
                        DataAll.InsertAll("HanhTrinh", dataview[gvLenhChoDuyet.FocusedRowHandle]["MaSanPham"]
                                                       + "," + Convert.ToInt32(PhongBan.PhongLenh)
                                                       + "," + dataview[gvLenhChoDuyet.FocusedRowHandle]["SoChungTu"]
                                                       + "," + Convert.ToInt32(TrangThai.LenhSanXuat_ChotLenh)
                                                       + "," + dataview[gvLenhChoDuyet.FocusedRowHandle]["NguoiLapLenh"]
                                                       + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                                                       + "," + dataview[gvLenhChoDuyet.FocusedRowHandle]["SoLuong"]
                                                       + "," + Convert.ToDateTime(dataview[gvLenhChoDuyet.FocusedRowHandle]["SXTuNgay"].ToString()).ToString("MM/dd/yyyy")
                                                       + "," + Convert.ToDateTime(dataview[gvLenhChoDuyet.FocusedRowHandle]["SXDenNgay"].ToString()).ToString("MM/dd/yyyy"));
                    }
                    else
                    {
                        DataAll.Update("HanhTrinh", "NgayThang = '" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff") + "' WHERE SoChungTu='" +
                                       dataview[gvLenhChoDuyet.FocusedRowHandle]["SoChungTu"]
                                       + "' AND TrangThai=" + Convert.ToInt32(TrangThai.LenhSanXuat_ChotLenh));
                    }
                }
                MessageBox.Show(message.Equals("success") ? "Duyệt lệnh thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void btnKhongDuyet_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvLenhChoDuyet.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn từ chối duyệt lệnh này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("LenhSanXuat", "TrangThai =" + Convert.ToInt32(TrangThai.LenhSanXuat_TuChoiDuyet)  + " WHERE ID =" + dataview[gvLenhChoDuyet.FocusedRowHandle]["ID"]);
                MessageBox.Show(message.Equals("success") ? "Từ chối duyệt lệnh thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void gvLenh_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvLenhChoDuyet_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvLenhChoDuyet_DoubleClick(object sender, EventArgs e)
        {
            if (gvLenhChoDuyet.FocusedRowHandle >= 0)
            {
                Lss_LapLenh.MaID = gvLenhChoDuyet.Columns["ID"].View.GetRowCellDisplayText(gvLenhChoDuyet.FocusedRowHandle, "ID");
                Lss_LapLenh.xem = Convert.ToInt32(gvLenhChoDuyet.GetDataRow(gvLenhChoDuyet.FocusedRowHandle)["TrangThai"]) == Convert.ToInt32(TrangThai.LenhSanXuat_ChoDuyet);
                var lss = new Lss_LapLenh();
                lss.ShowDialog();
                LoadData();
                Lss_LapLenh.MaID = "0";
            }
        }

        private void gvLenhChoDuyet_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }

        private void bntXoaLenh_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa lệnh này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.DeleteLenh(gvLenh.GetRowCellValue(gvLenh.FocusedRowHandle, "IdSanPham").ToString());
                MessageBox.Show(message.Equals("success") ? "Xóa lệnh thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void dtTuNgay_Leave(object sender, EventArgs e)
        {
            LoadData();
        }

        private void dtDenNgay_Leave(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
